function Invoke-StatSPOnlineWebRequest
{
    <# 
        .SYNOPSIS
            Retrieves SharePoint Online tenant account details from the associated account configuration file and establishes a connection to the tenant

        .DESCRIPTION
            Part of the SharePoint Total Automation Toolkit (STAT). This function reads in a single data row from SharePoint Online Account file when called. It then establishes a connection to the SharePoint Online tenant.
            
            If the configuration files are stored in the standard path and structure, you have the option of specifying each discrete parameter. Alternatively you can provide the full path to the configuration directory.
 
        .EXAMPLE
            Invoke-StatSPOnlineWebRequest -Company Contoso -Project SharePointOnline -Release 1.0.0.0 -Environment Development

            If using the default configuration file path and structure, you can provide discrete parameters to each subdirectory.

        .EXAMPLE
            Invoke-StatSPOnlineWebRequest -ConfigPath 'C:\ProgramData\SharePoint Total Automation Toolkit\Configuration Files\Contoso\SharePointOnline\1.0.0.0\Development'

            Using the -ConfiPath parameter allows you to directly specify the full path to the configuration files that control the process.

        .EXAMPLE
            $myFullPath | Invoke-StatSPOnlineWebRequest

            This function accepts pipeline input for the -ConfiPath parameter which allows you to directly specify the full path to the configuration files that control the process.

        .PARAMETER StatAccountLookup
            The internal STAT name that this function will use when querying the tenant account list for account details such as the username and password. If no account is provided, the SharePoint Administrator account will be used.

        .PARAMETER Company
            The company subdirectory (e.g. Contoso)
            Part of the overall path to the configuration files that control the installation process

        .PARAMETER Project
            The project subdirectory (e.g. SharePointOnline)
            Part of the overall path to the configuration files that control the installation process

        .PARAMETER Release
            The release subdirectory (e.g. 1.0.0.0)
            Part of the overall path to the configuration files that control the installation process

        .PARAMETER Environment
            The target environment subdirectory (e.g. Development)
            Part of the overall path to the configuration files that control the installation process

        .PARAMETER ConfigPath
            The full path to the configuration files that control the installation process
            (e.g. 'C:\ProgramData\SharePoint Total Automation Toolkit\Configuration Files\Contoso\SharePointOnline\1.0.0.0\Development')

    #>
    [CmdletBinding(DefaultParametersetName="DiscreteValues")]
    param([parameter(ParameterSetName = "DiscreteValues", 
                     Mandatory = $true,
                     HelpMessage = "The company subdirectory (e.g. Contoso). Part of the overall path to the configuration files that control the installation process")]
                     [string]$Company,
          [parameter(ParameterSetName = "DiscreteValues", 
                     Mandatory = $true,
                     HelpMessage = "The project subdirectory (e.g. Intranet). Part of the overall path to the configuration files that control the installation process")]
                     [string]$Project,
          [parameter(ParameterSetName = "DiscreteValues", 
                     Mandatory = $true,
                     HelpMessage = "The release subdirectory (e.g. 1.0.0.0). Part of the overall path to the configuration files that control the installation process")]
                     [string]$Release,
          [parameter(ParameterSetName = "DiscreteValues", 
                     Mandatory = $true,
                     HelpMessage = "The target environment subdirectory (e.g. Development). Part of the overall path to the configuration files that control the installation process")]
                     [string]$Environment,
          [parameter(ParameterSetName = "FullPath", 
                     Mandatory = $true,
                     ValueFromPipeline=$true,
                     ValueFromPipelineByPropertyName=$true, 
                     HelpMessage = "The full path to the configuration files that control the installation process. (e.g. 'C:\ProgramData\SharePoint Total Automation Toolkit\Configuration Files\Contoso\Intranet\1.0.0.0\Development'")]
                     [string]$ConfigPath,
          [parameter(ParameterSetName = "DiscreteValues", 
                     Mandatory = $false,
                     HelpMessage = "The internal STAT name that this function will use when querying the tenant account list for account details such as the username and password")]
          [parameter(ParameterSetName = "FullPath", 
                     Mandatory = $false,
                     HelpMessage = "The internal STAT name that this function will use when querying the tenant account list for account details such as the username and password")]
                     [string]$StatAccountLookup="SharePoint Administrator",
          [parameter(ParameterSetName = "DiscreteValues", 
                     Mandatory = $true,
                     HelpMessage = "The URI to the API (e.g. /_api/web/title)")]
          [parameter(ParameterSetName = "FullPath", 
                     Mandatory = $true,
                     HelpMessage = "The URI to the API (e.g. /_api/web/title)")]
                     [string]$ApiUri,
          [parameter(ParameterSetName = "DiscreteValues", 
                     Mandatory = $false,
                     HelpMessage = "The content of the request for a Post operation")]
          [parameter(ParameterSetName = "FullPath", 
                     Mandatory = $false,
                     HelpMessage = "The content of the request for a Post operation")]
                     [Hashtable]$Body,
          [parameter(ParameterSetName = "DiscreteValues", 
                     Mandatory = $false,
                     HelpMessage = "A web session retreived from calling Get-StatSPOnlineWebSession")]
          [parameter(ParameterSetName = "FullPath", 
                     Mandatory = $false,
                     HelpMessage = "A web session retreived from calling Get-StatSPOnlineWebSession")]
                     [Microsoft.PowerShell.Commands.WebRequestSession]$Session)

    switch ($PSCmdlet.ParameterSetName)
    {
        "DiscreteValues"
        {
            $ConfigPath = $env:ProgramData + "\SharePoint Total Automation Toolkit\Configuration Files\" + $Company + "\" + $Project + "\" + $Release + "\" + $Environment
        }    
        "FullPath"
        {
        }
    }

    # Version Compatibility Check Variable
    $firstCompatibleVersion = "0.1.0.0"

    # Conduct Version Compatibility Check
    if ((ConfigVersionIsCompatible $firstCompatibleVersion $ConfigPath))
    {
        $tenantSiteInfo = Get-StatSPOnlineTenantInfo -ConfigPath $ConfigPath 
        $tenantSiteUrl = $tenantSiteInfo.BaseUri

        # If a session was not provided for the request, establish one
        if ($Session -eq $null)
        {
            $sessionInfo  = Get-StatSPOnlineWebSession -ConfigPath $ConfigPath -StatAccountLookup $StatAccountLookup
            $Session = $sessionInfo.Session
        }

        $cmdUri = $tenantSiteUrl + $ApiUri
        $headers = @{"accept" = "application/json; odata=verbose";
                     "X-RequestDigest" = $sessionInfo.FormDigestValue
                    }

        $data = Invoke-WebRequest -Uri $cmdUri -Headers $headers -WebSession $Session -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer)

        $resultJson = $data.Content | ConvertFrom-Json

        return $resultJson.d
    }
    else
    {
        Write-Host "     ERROR:" -ForegroundColor Red
        Write-Host "     This script requires configuration files at version" $firstCompatibleVersion "or higher." -ForegroundColor Red
        Write-Host "     Use the Get-Help command for a complete list of the configuration files used by this script." -ForegroundColor Red

    }
}